package zhao.tx.eecs.Tools;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;


/**
 * Created by ishishuji on 14-9-25.
 */
public class FragmentTools {

    public static void refreshFragment ( FragmentManager fragmentManager, String tag ) {
        Fragment frg = null;
        frg = fragmentManager.findFragmentByTag ( tag );
        final FragmentTransaction ft = fragmentManager.beginTransaction ();
        ft.detach ( frg );
        ft.attach ( frg );
        ft.commit ();
    }

    public static void replaceFragment ( FragmentManager fragmentManager, String currentTag, String replaceTag ) {
        Fragment frg = null, replaceFrg = null;
        frg = fragmentManager.findFragmentByTag ( currentTag );
        replaceFrg = fragmentManager.findFragmentByTag ( replaceTag );
        final FragmentTransaction ft = fragmentManager.beginTransaction ();
        ft.detach ( frg );
        ft.attach ( replaceFrg );
        ft.commit ();
    }


    public static void refreshFragment ( FragmentManager fragmentManager, int id ) {
        Fragment frg = null;
        frg = fragmentManager.findFragmentById ( id );
        final FragmentTransaction ft = fragmentManager.beginTransaction ();
        ft.detach ( frg );
        ft.attach ( frg );
        ft.commit ();
    }

    public static void replaceFragment ( FragmentManager fragmentManager, int currentId, int replaceId ) {
        Fragment frg = null, replaceFrg = null;
        frg = fragmentManager.findFragmentById ( currentId );
        replaceFrg = fragmentManager.findFragmentById ( replaceId );
        final FragmentTransaction ft = fragmentManager.beginTransaction ();
        ft.detach ( frg );
        ft.attach ( replaceFrg );
        ft.commit ();
    }

    public static void replaceFragment ( FragmentManager fragmentManager, int currentId,Fragment replaceFragment ) {
        FragmentTransaction transaction = fragmentManager.beginTransaction ();
// Replace whatever is in thefragment_container view with this fragment,
// and add the transaction to the backstack
        transaction.replace ( currentId, replaceFragment );
        transaction.addToBackStack ( null );
//提交修改
        transaction.commit ();
    }

}
